define(function (require, exports, module)
{
  var $ = require('jquery');
  require('json2');
  var jxbaseinfo = require('jxbaseinfo');

  var $fileOperate =
          {
            fileNum: 0,
            fileIndex: 100,
            submitFlag: true,
            fileMaxNum: 0,
            updateMark: 1,
            fileSuffixes: "",
            checkFile: function (fileName)
            {
              if ((this.fileSuffixes == "") || (this.fileSuffixes == "*"))
                return true;

              eval("var reg=/([^\s]+(?=\.(" + this.fileSuffixes + "))\\.\\2)/gi;");
              return reg.test(fileName);
            },
            checkUploadFile: function ()
            {
              for (var i = 0; i < $("input[type=file]").length; i++)
              {
                if ($("input[type=file]").eq(i).val() == '')
                  return false;

                if (!this.checkFile($("input[type=file]").eq(i).val()))
                  return false;
              }
              return true;
            },
            addNewFile: function ()
            {
              if ((this.fileMaxNum > 0) && (this.fileNum >= this.fileMaxNum))
                return;

              this.fileNum++;
              this.fileIndex++;
              var url = jxbaseinfo.path + "common/html/common-kit-filecontent@" + this.fileIndex;
						 $.ajaxSettings.async = false;
              $.get(url, function (data)
              {
                $('ul#ulFiles').append(data);
              });
							$.ajaxSettings.async = true;
               $('#file'+this.fileIndex).click();		
            },
            removeFile: function ()
            {
              this.fileNum--;
            },
            getFileSizeInfo:function(size)
                {
                  var strSize ;
                  if (size <1024*1024)
                    strSize = Math.round(size/1024) + "KB";
                  else if (size <1024*1024*1024)
                    strSize = Math.round(size*100/(1024*1024))/100 + "MB";
                  else
                    strSize = Math.round(size*100/(1024*1024*1024))/100 + "GB";

                  return strSize 
                },
            getObjectURL:function(file) 
            {
            var url = null ;
            if (window.createObjectURL!=undefined) { // basic
            url = window.createObjectURL(file) ;
            } else if (window.URL!=undefined) { // mozilla(firefox)
            url = window.URL.createObjectURL(file) ;
            } else if (window.webkitURL!=undefined) { // webkit or chrome
            url = window.webkitURL.createObjectURL(file) ;
            }
            return url ;
            }		            
          };

  $(function ()
  {
    if ($("#jx-txt-tips").length == 0)
    {
      $.ajax({
        async: false,
        url: jxbaseinfo.path + 'common/kit/check_tip.html', //这里是静态页的地址
        type: "GET", //静态页用get方法，否则服务器会抛出405错误
        success: function (data) {
          $(document.body).append(data);
        }
      });
    }

    $(document).on("click", "input[type=button]", function ()
    {
      if ($(this).attr("tag") == "fileadd")
        $fileOperate.addNewFile();
    });
    $(document).on("click", ".upload_mark", function ()
    {
      $(this).parent().remove();
      $fileOperate.removeFile();
    });	
    $(document).on("click", "[tag=fileclick]", function ()
    {
      $(this).parent().find("input[type=file]").click() ;
    });   
    $(document).on("change", "input[type=file]", function ()
    {
      $(this).parent().find(".upload_file_size").html($fileOperate.getFileSizeInfo($(this).get(0).files[0].size));
      var filename = $(this).get(0).files[0].name ;
      $(this).parent().find(".upload_file_name").html(filename);
      if ($(this).get(0).files[0].type=="image/jpeg")
      {
        $(this).parent().find("img").show() ;
        $(this).parent().find(".upload_no_preview").hide()
        var objUrl = $fileOperate.getObjectURL($(this).get(0).files[0]) ; 
        if (objUrl) 
          $(this).parent().find("img").attr("src", objUrl) ;
      }
      else
      {
        $(this).parent().find("img").hide() ;
        var index = filename.lastIndexOf(".");
        var suffix = filename.substr(index+1);
        $(this).parent().find(".upload_no_preview").show() ;
        $(this).parent().find(".upload_no_preview").html(suffix + jxbaseinfo.upload_no_preview) ;
      }
    });	

    
  });
  return $fileOperate;
});